using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL.Fabric
{
    public class BreakoutBlock : Block
    {
        int iOutputs = 0;
        int iInputWidth = 0;

        public BreakoutBlock(string psName, params int[] piWidths)
            : base(psName,"BREAKOUT")
        {
            iOutputs = 0;
            foreach (int iWidth in piWidths)
            {
                iOutputs++;
                string sName = "OUT" + iOutputs.ToString();
                CreateOutput(sName, iWidth);
                iInputWidth += iWidth;
            }
            CreateInput("IN", iInputWidth);
        }

        internal override void GenerateStructure()
        {
            NodeVector nvIn = ParentComponent.CreateNodeVector(Name + "_DATA_I", iInputWidth);
            Input oInput = Inputs["IN"];
            oInput.LinkNodeVector(nvIn);
            int iLeft = 0;
            for (int iOutput = 1; iOutput <= iOutputs; iOutput++)
            {
                string sName = "OUT" + iOutput.ToString();
                Output oOutput = Outputs[sName];
                NodeVector nvOut = ParentComponent.CreateNodeVector(Name + "_DATA" + iOutput.ToString() + "_O", oOutput.Width);
                oOutput.LinkNodeVector(nvOut);
                oInput.NodeVector[iLeft, iLeft + oOutput.Width - 1].Join(nvOut);
                iLeft += oOutput.Width;
            }
        }

    }
}
